#include "intCounter.h"

// Create an empty list.
intCounter::intCounter()
{
   head = NULL;
}

// Add to the count of an integrer l.
void intCounter::increment(int l)
{
   // Add to an empty list.
   if (head == NULL) {
      head = new numNode();

      head -> num = l;
      head -> count = 1;
      head -> next = NULL;
      }
   
   // Non-empty list
   else {
      numNode* tip = head;

      while (tip -> next != NULL && tip -> num != l)
         tip = tip -> next;

      // Level is already in list.
      if (tip -> num == l)
         tip -> count++;

      // Level is not in the list; add it to the end.
      else {
         numNode* temp = new numNode();

         temp -> num = l;
	 temp -> count = 1;
         temp -> next = NULL;

	 tip -> next = temp;
         }
      }
}

// Decrease the count for a number l.
// Should throw an error if the count is 0 or the number is not in
// the list.
void intCounter::decrement(int l)
{
   numNode* tip = head;

   while (tip != NULL && tip -> num != l)
      tip = tip -> next;

   if (tip == NULL || tip -> count == 0)
     // cout << "Error! Trying to decrement from a number not in the list." << endl;
     tip = tip;

   else
      tip -> count--;
}

// Return the count for a certain int.
int intCounter::count(int n)
{
   numNode* tip = head;

   while (tip != NULL && tip -> num != n)
      tip = tip -> next;

   if (tip == NULL)
      return 0;

   else
      return tip -> count;
}

// Print the list to standard output.
void intCounter::printCounter()
{
   numNode* tip = head;

   while (tip != NULL) {
      cout << tip -> num << " " << tip -> count << endl;
      tip = tip -> next;
      }
}
